package com.cw.jeeyt.core.impl.facadeExcel;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cw.jeeyt.api.dto.facadeExcel.ApiEnum;
import com.cw.jeeyt.api.dto.facadeExcel.ApiEnumItem;

/**
 * @description: 枚举解析工具类
 * @author: chenw
 * @create: 2018-11-04 00:09
 */
public class EnumAnalysisCore {
    private static Logger logger = LoggerFactory.getLogger(EnumAnalysisCore.class);

    /**
     * 解析枚举类
     */
    public static List<ApiEnum> analysisEnum(Map<String, Class> classFileInfos, List<ApiEnum> enums) {
        if (classFileInfos == null || classFileInfos.isEmpty()) {
            return enums;
        }
        for (ApiEnum apiEnum : enums) {
            if (true == apiEnum.isInit()) {
                continue;
            }
            Set keySet = classFileInfos.keySet();
            Iterator iterator = keySet.iterator();
            while (iterator.hasNext()) {
                String key = (String) iterator.next();
                Class c = classFileInfos.get(key);
                if (key.equals(apiEnum.getClassName())) {
                    List<ApiEnumItem> apiEnumItems = new ArrayList<>();
                    logger.info("加载枚举信息：{}", apiEnum.getClassName());
                    Object[] objects = c.getEnumConstants();
                    Method getCode = null;
                    Method getName = null;
                    try {
                        getCode = c.getMethod("getCode");
                    } catch (Exception e) {
                        try {
                            getCode = c.getMethod("getType");
                        } catch (Exception e1) {
                            try {
                                getCode = c.getMethod("findByCode");
                            } catch (Exception e2) {
                                logger.error("枚举【{}】没有getCode、getType、findByCode方法");
                            }
                        }
                    }
                    try {
                        getName = c.getMethod("getDescription");
                    } catch (Exception e) {
                        try {
                            getName = c.getMethod("getDesc");
                        } catch (Exception ee) {
                            try {
                                getName = c.getMethod("getMsg");
                            } catch (Exception e1) {
                                try {
                                    getName = c.getMethod("getRemark");
                                } catch (Exception e2) {
                                    try {
                                        getName = c.getMethod("getMessage");
                                    } catch (Exception e3) {
                                        try {
                                            getName = c.getMethod("getName");
                                        } catch (Exception e4) {
                                            logger.error("枚举【{}】没有getDescription、getDesc、getMsg、getRemark、getMessage方法");
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (objects != null && objects.length > 0) {
                        for (Object obj : objects) {
                            // 3.调用对应方法，得到枚举常量中字段的值
                            ApiEnumItem item = new ApiEnumItem();
                            try {
                                item.setCode(getCode.invoke(obj).toString());
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            }
                            try {
                                item.setDesc(getName.invoke(obj).toString());
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            }
                            item.setComment(obj.toString());
                            apiEnumItems.add(item);
                            try {
                                System.out.println("code=" + getCode.invoke(obj) + "; name=" + getName.invoke(obj));
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    apiEnum.setItemList(apiEnumItems);
                    apiEnum.setInit(true);
                }
            }
        }
        return enums;
    }

    public static void main(String[] args) {
        // List<String> jarFilePaths = new ArrayList<>();
        // jarFilePaths.add("D:/Repository/maven/com/cw/accounting/hsjry-asset-facade-pojo/2.1
        // .0-SNAPSHOT/hsjry-asset-facade-pojo-2.1.0-SNAPSHOT.jar");
        // jarFilePaths.add("C:/yunrong/yunrong_project/LoanV2.1/common/model/target/model-2.1.0-SNAPSHOT.jar");
        // List<ApiEnum> enums = new ArrayList<>();
        // ApiEnum apiEnum = new ApiEnum();
        // apiEnum.setClassName("com.hsjry.asset.facade.enums.BatchTradeType");
        // apiEnum.setEnumCode("BatchTradeType");
        // apiEnum.setEnumDesc("交易类型");
        // enums.add(apiEnum);
        // analysisEnum(jarFilePaths,enums);
    }
}
